package com.liming.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.liming.model.GoodsType;
import com.liming.utils.DataSourceUtils;
/**
 * 
 * @author 吕腾飞
 * @version 
 */
public class GoodsTypedao {
	QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
	public List<GoodsType> getType() {
		List<GoodsType> types=null;
		String sql="select * from goodstype ";
		try {
			types=qr.query(sql, new BeanListHandler<>(GoodsType.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return types;
	}
	public GoodsType getTypeById(int id) {
		try {
			return qr.query("select * from goodstype where typeid = ?", new BeanHandler<>(GoodsType.class),id);
		} catch (SQLException e) {
			e.printStackTrace();
			return GoodsType.NULLTYPE;
		}	
	}
	public List<GoodsType> getTypesByPid(int pid) {
		try {
			return qr.query("select * from goodstype where pid = ?", new BeanListHandler<>(GoodsType.class),pid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<>();
		}
	}
	
	public List<Integer> getTypeidsByPid(int pid) {
		try {
			return qr.query("select typeid from goodstype where pid = ?", new BeanListHandler<>(Integer.class),pid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<>();
		}
	}
		
}
